package coms.dataaccess.hibernate;

import java.util.List;
import java.util.Set;

import org.hibernate.Query;

import coms.dataaccess.ConferenceDAO;
import coms.dataaccess.DAOFactory;
import coms.dataaccess.TransactionDAO;
import coms.dataaccess.UserDao;
import coms.datatransfer.ConferenceItemDTO;
import coms.datatransfer.ConferenceItem_User;
import coms.datatransfer.TransactionDTO;
import coms.datatransfer.UserDTO;

public class TransactionDAOImpl extends AbstractDAO implements TransactionDAO {
	
	private ConferenceDAO conferenceDAO = null;
	private UserDao userDAO = null;
	private DAOFactory factory = null;
	
	@Override
	public void initialize() {
		factory = DAOFactory.getInstance();
		conferenceDAO = factory.getConferenceDAO();
		userDAO = factory.getUserDAO();
	}
	
	@Override
	public TransactionDTO makePayment(TransactionDTO transaction){
		super.saveOrUpdate(transaction);
		
		ConferenceItemDTO conferenceItemDTO = conferenceDAO.findConferenceItemByID(transaction.getConferenceItemID());
		UserDTO userDTO = userDAO.findUserByID(transaction.getUserID());
		System.out.println(transaction.getPaidAmount());
		System.out.println(conferenceItemDTO.getItemFee());
		if(transaction.getPaidAmount() >= conferenceItemDTO.getItemFee()){
			Set<ConferenceItem_User> set = conferenceItemDTO.getListOfParticipants();
			for(ConferenceItem_User cu: set){
				if(userDTO.getUserID() == cu.getUserDTO().getUserID()){
					cu.setStatus("Eligible");
					super.saveOrUpdate(conferenceItemDTO);
				}
			}
		}	
		
		return transaction;
	}

	@SuppressWarnings("unchecked")
	@Override
	public  List<TransactionDTO> getAllTransactions(int ConferenceItemID,int  userID) {
		// TODO Auto-generated method stub
		
		System.out.println( "Hi"+super.findTransaction(ConferenceItemID, userID).size());
		
		return super.findTransaction(ConferenceItemID, userID);
		
		
		
	}
	




}
